package tool;

public class Timer {

    public static final float NANO_PER_SEC = 1E9f;
    private long timer;

    public Timer() {
        this(false);
    }

    public Timer(boolean startNow) {
        if (startNow) {
            timer = getSystemTime();
        } else {
            timer = 0L;
        }
    }

    public boolean isRunning() {
        return (timer != 0L);
    }

    public void start() {
        timer = getSystemTime();
    }

    public void resetAndStart() {
        if (isRunning()) {
            timer = getSystemTime();
        }
    }

    public float getTimePassed() {
        if (isRunning()) {
            return (getSystemTime() - timer) / NANO_PER_SEC;
        } else {
            return 0;
        }
    }

    public void moveBackBy(float timeSec) {
        long deltaTime = (long) (NANO_PER_SEC * timeSec + 0.5f);
        timer += deltaTime;
    }

    public void stop() {
        timer = 0L;
    }

    public float stopAndReturnTime() {
        float result = getTimePassed();
        stop();
        return result;
    }

    public static long getSystemTime() {
        return System.nanoTime();
    }

}
